﻿using DevExpress.XtraReports.UI;
using DevTeam;
using System;
using System.Drawing;

namespace ConfExpert.Forms.Rapor
{
    public partial class xrIrsaliye : DevExpress.XtraReports.UI.XtraReport
    {
        public xrIrsaliye()
        {
            InitializeComponent();
        }
        public xrIrsaliye(int irsaliyeId, int kesilecekToplar)
            : this()
        {
            myData1.Merge(Session.DataManager.ExecuteDataSet("GetCikisEmriByIrsaliyeId", irsaliyeId, kesilecekToplar));
            myData1.Irsaliye.Merge(myData1.Tables["Table"]);
            myData1.IrsaliyeLines.Merge(myData1.Tables["Table1"]);
            myData1.IrsaliyeLinesDetail.Merge(myData1.Tables["Table2"]);

            xrIrsaliyeDetail xrSubDetail1 = (xrIrsaliyeDetail)xrSubreport1.ReportSource;
            xrSubDetail1.fillTables(myData1.IrsaliyeLinesDetail, kesilecekToplar);

            tcAtolye.Text = myData1.Irsaliye.Rows[0]["Atelye"].ToString();
            tcCikan.Text = myData1.Irsaliye.Rows[0]["Cikan"].ToString();
            tcCikisTarihi.Text = myData1.Irsaliye.Rows[0]["CikisTarihi"].ToString();

            lblHeader.Text = kesilecekToplar == 0 ? "İrsaliye Top Detayı" : "Çeki Listesi";
            KesilecekTop = kesilecekToplar;
        }
        int KesilecekTop;
        public xrIrsaliye(string cikisEmriId, int kesilecekToplar, string SpName)
            : this()
        {
            myData1.Merge(Session.DataManager.ExecuteDataSet(SpName, cikisEmriId, kesilecekToplar));
            myData1.Irsaliye.Merge(myData1.Tables["Table"]);
            myData1.IrsaliyeLines.Merge(myData1.Tables["Table1"]);
            myData1.IrsaliyeLinesDetail.Merge(myData1.Tables["Table2"]);

            xrIrsaliyeDetail xrSubDetail1 = (xrIrsaliyeDetail)xrSubreport1.ReportSource;
            xrSubDetail1.fillTables(myData1.IrsaliyeLinesDetail, kesilecekToplar);

            headerKesilecek.Text = SpName == "GetParcaCikisEmri" ? "Böl.Top" : "BölmeMi";
            tcAtolye.Text = myData1.Irsaliye.Rows[0]["Atelye"].ToString();
            tcCikan.Text = myData1.Irsaliye.Rows[0]["Cikan"].ToString();
            tcCikisTarihi.Text = myData1.Irsaliye.Rows[0]["CikisTarihi"].ToString();
            lblHeader.Text = kesilecekToplar == 0 ? "İrsaliye Top Detayı" : "Çeki Listesi";
            KesilecekTop = kesilecekToplar;

        }

        private void xrSubreport1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ((xrIrsaliyeDetail)((XRSubreport)sender).ReportSource).PrdId.Value = Convert.ToInt32(DetailReport1.GetCurrentColumnValue("ProductId"));
            ((xrIrsaliyeDetail)((XRSubreport)sender).ReportSource).CksEmriId.Value = Convert.ToInt32(DetailReport1.GetCurrentColumnValue("CikisEmriId"));
        }

        private void xrTable10_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            string ProductId = DetailReport1.GetCurrentColumnValue("ProductId").ToString();
            string CikisEmriId = DetailReport1.GetCurrentColumnValue("CikisEmriId").ToString();

            int a = 0;
            tblKumas.HeightF = 127F;
            Detail2.HeightF = 127F;
            if (myData1.IrsaliyeLinesDetail.Rows.Count > 0)
                a = myData1.IrsaliyeLinesDetail.Select("ProductId=" + ProductId + " and CikisEmriId=" + CikisEmriId).Length;
            if (a > 2)
            {
                tblKumas.HeightF = a * 63.5F;
            }
        }


        private void xrIrsaliyeDetay_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (KesilecekTop == 0)
            {
                headerKesilecek.Dispose();
                detailKesilecek.Dispose();
                //xrTableCell1.WidthF=837.14F;
                detailKesilecek2.Dispose();
                HeaderRaf.Dispose();
                detailRaf.Dispose();
                detailRaf2.Dispose();
                headerMetraj.Text = "Metraj";
                this.detailMetraj.BorderColor = System.Drawing.Color.Black;
                this.detailMetraj.Borders = ((DevExpress.XtraPrinting.BorderSide)((DevExpress.XtraPrinting.BorderSide.Right | DevExpress.XtraPrinting.BorderSide.Bottom)));
                tbAciklama.Dispose();
            }
            if (myData1.Irsaliye.Rows.Count == 1)
            {
                xrLine1.Dispose();
                xrLine2.Dispose();
            }
        }

        private void xrTable1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            string ProductId = DetailReport1.GetCurrentColumnValue("ProductId").ToString();
            string CikisEmriId = DetailReport1.GetCurrentColumnValue("CikisEmriId").ToString();

            int a = 0;
            xrTable1.HeightF = 126F;
            Detail2.HeightF = 127F;
            if (myData1.IrsaliyeLinesDetail.Rows.Count > 0)
                a = myData1.IrsaliyeLinesDetail.Select("ProductId=" + ProductId + " and CikisEmriId=" + CikisEmriId).Length;
            if (a > 2)
            {
                xrTable1.HeightF = a * 63.5F - 1;
            }
        }

        private void xrLabel5_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            int adet = myData1.IrsaliyeLinesDetail.Rows.Count;
            xrLabel5.Text = adet.ToString();
        }

        private void xrLabel4_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (myData1.IrsaliyeLinesDetail.Rows.Count > 0)
            {
                decimal metraj = Convert.ToDecimal(myData1.IrsaliyeLinesDetail.Compute("Sum(qty)", ""));
                xrLabel4.Text = metraj.ToString();
            }
        }

        private void xrPictureBox2_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            xrPictureBox2.WidthF = xrTableCell19.WidthF;
        }

        private void xrTableCell20_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            //Resize ı düzgün yapan tek fonksiyon !!!
            Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
            gr.PageUnit = GraphicsUnit.Millimeter;
            int factor = 10;

            XRLabel label = (XRLabel)sender;

            float newSize = 12f;
            Font defaultFont = new System.Drawing.Font("Calibri", newSize, FontStyle.Bold);

            float stringWidth = gr.MeasureString(label.Text, defaultFont).Width;
            float labelWidth = label.WidthF;


            if (labelWidth < stringWidth * factor)
                newSize = defaultFont.Size * label.Width / stringWidth / factor;


            label.Font = new Font(defaultFont.FontFamily, newSize, FontStyle.Bold);
            gr.Dispose();
        }

        private void tbAciklama_PrintOnPage(object sender, PrintOnPageEventArgs e)
        {
            if (e.PageCount > 0)
            {
                if (e.PageIndex != e.PageCount - 1 || myData1.IrsaliyeLinesDetail.Select("AlternatifId is not null").Length == 0)
                {
                    e.Cancel = true;
                    //this.xrTable4.Dispose();
                }
            }
        }

        private void Detail2_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            Detail2.HeightF = 127F;
        }


    }
}
